package com.ss.ttm.player;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
import android.view.Surface;

import java.io.File;

import inline.ScreenshotListener;

public class TTPlayer {
    private static boolean IsErrored = false;
    private static final int KITKAT = 19;
    private static final String TAG = TTPlayer.class.getSimpleName();
    private static String mAppPath;
    private static String mCachePath;
    private Context mContext;
    private static String mCrashPath;
    private long mHandle;
    private long mId;
    private static boolean mIsIPCPlayer = false;
    private Notifier mNotifyer;
    private long mNotifyerState;
    private ScreenshotListener mScreenshotListener;
    private static int mSupportSampleRateNB = 0;
    private static int[] mSupportSampleRates;
    private int mTracker;

    static {
        synchronized (TTPlayer.class) {
            loadLibrary();
        }
    }

    public TTPlayer(Context context, long arg4) throws Exception {
        this.mNotifyerState = 2147483647;
        this.mTracker = 0;
        this.mContext = context;
        this.create(arg4);
    }

    private static final native void _close(long arg0);

    private final native long _create(Context arg1, int arg2, String arg3);

    private static final native String _getAppPath();

    private static final native int _getCurrentPosition(long arg0);

    private static final native int _getDuration(long arg0);

    private static final native int _getIntValue(long arg0, int arg1, int arg2);

    private static final native long _getLongValue(long arg0, int arg1, long arg2);

    private static final native String _getStringValue(long arg0, int arg1);

    private static final native int _getVideoHeight(long arg0);

    private static final native int _getVideoWidth(long arg0);

    private static final native int _isLooping(long arg0);

    private static final native int _isPlaying(long arg0);

    private static final native void _mouseEvent(long arg0, int arg1, int arg2, int arg3);

    private static final native int _pause(long arg0);

    private static final native int _prepare(long arg0);

    private static final native void _prevClose(long arg0);

    private static final native void _registerPlayerInfo();

    private static final native void _release(long arg0);

    private static final native int _reset(long arg0);

    private static final native void _rotateCamera(long arg0, float arg1, float arg2);

    private static final native int _seek(long arg0, int arg1);

    private static final native void _setCacheFile(long arg0, String arg1, int arg2);

    private static final native void _setDataSource(long arg0, String arg1);

    private static final native int _setIntValue(long arg0, int arg1, int arg2);

    private static final native int _setLongValue(long arg0, int arg1, long arg2);

    private static final native void _setLooping(long arg0, int arg1);

    private static final native int _setStringValue(long arg0, int arg1, String arg2);

    private static final native void _setSupportFormatNB(int arg0);

    private static final native void _setSupprotSampleRates(int[] arg0, int arg1);

    private static final native int _setSurfaceValue(long arg0, long arg1);

    private static final native int _setVideoSurface(long arg0, Surface arg1);

    private static final native void _setVolume(long arg0, float arg1, float arg2);

    private static final native int _start(long arg0);

    private static final native void _stop(long arg0);

    private static final native void _takeScreenshot(long arg0);


    public int close() {
        if (this.mHandle == 0) {
            return -1;
        } else {
            _close(this.mHandle);
            return 0;
        }
    }

    private void create(long id) throws Exception {
        this.mNotifyer = null;
        int isIPCPlayer = mIsIPCPlayer ? 1 : 0;
        this.mHandle = this._create(mContext, isIPCPlayer, mAppPath);
        this.mId = id;
        if (this.mHandle == 0) {
            throw new Exception("create native player is fail.");
        }
    }

    public static final String getAppPath() {
        return mAppPath;
    }

    public static final String getCachePath() {
        return mCachePath;
    }

    public Context getContext() {
        return this.mContext;
    }

    public static final String getCrashPath() {
        return mCrashPath;
    }

    public final long getHandle() {
        return this.mHandle;
    }

    public int getIntOption(int arg3, int arg4) {
        int v0;
        switch (arg3) {
            case 1: {
                v0 = _getDuration(this.mHandle);
                break;
            }
            case 2: {
                v0 = _getCurrentPosition(this.mHandle);
                break;
            }
            case 3: {
                v0 = _getVideoWidth(this.mHandle);
                break;
            }
            case 4: {
                v0 = _getVideoHeight(this.mHandle);
                break;
            }
            case 5: {
                v0 = _isLooping(this.mHandle);
                break;
            }
            case 6: {
                v0 = _isPlaying(this.mHandle);
                break;
            }
            case 11: {
                v0 = _getIntValue(this.mHandle, arg3, arg4);
                break;
            }
            case 51: {
                v0 = this.mTracker;
                break;
            }
            default: {
                v0 = _getIntValue(this.mHandle, arg3, arg4);
                break;
            }
        }

        return v0;
    }

    public long getLongOption(int arg3, long arg4) {
        return arg3 == 50 ? this.mHandle : _getLongValue(this.mHandle, arg3, arg4);
    }

    public long getNativePlayer() {
        return this.mHandle;
    }

    public String getStringOption(int arg3) {
        return _getStringValue(this.mHandle, arg3);
    }

    public static boolean isError() {
        return IsErrored;
    }

    public static boolean isIPPlayer() {
        return mIsIPCPlayer;
    }

    private static final void loadLibrary() {
        String v0_2 = null;
        String v1 = null;
        int v0 = 3;
        try {
            if (!m.a(v0)) {
                v0_2 = v1;
            } else {
                m.e();
                v0_2 = m.f();
                if (v0_2 != null && !new File(v0_2).exists()) {
                    v0_2 = v1;
                }

                if (v0_2 == null) {
                } else {
                    System.load(v0_2);
                }
            }
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
        }
        if (v0_2 == null) {
            System.loadLibrary("ttmplayer");
        }
        try {
            if (Build.VERSION.SDK_INT >= 21) {
                com.ss.ttm.player.a.b();
            }

            if (mSupportSampleRateNB != 0) {
                return;
            }

            int[] v0_3 = new int[com.ss.ttm.player.a.a()];
            mSupportSampleRates = v0_3;
            mSupportSampleRateNB = com.ss.ttm.player.a.a(v0_3);
        } catch (Throwable throwable) {
            IsErrored = true;
            throwable.printStackTrace();
        }

    }

    public void mouseEvent(int arg3, int arg4, int arg5) {
        _mouseEvent(this.mHandle, arg3, arg4, arg5);
    }

    public final void onLogInfo(int arg8, int arg9, String arg10) {
        if (this.mNotifyer != null) {
            try {
                this.mNotifyer.onPlayLogInfo(this.mId, arg8, arg9, arg10);
            } catch (Throwable v0) {
            }
        }
    }

    public void onNotify(int arg9, int arg10, int arg11, String arg12) {
        if ((this.mNotifyerState >> arg10 & 1) == 1) {
            try {
                if (this.mNotifyer == null) {
                    return;
                }
                this.mNotifyer.onPlayerNotify(this.mId, arg9, arg10, arg11, arg12);
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
    }

    protected int pause() {
        int v0 = this.mHandle == 0 ? -1 : _pause(this.mHandle);
        return v0;
    }

    protected int prepare() {
        int v0 = this.mHandle == 0 ? -1 : _prepare(this.mHandle);
        return v0;
    }

    public int prevClose() {
        int v0;
        if (this.mHandle == 0) {
            v0 = -1;
        } else {
            _setVideoSurface(this.mHandle, null);
            v0 = 0;
        }

        return v0;
    }

    public static void registerPlayerInfo() {
        _registerPlayerInfo();
    }

    protected void release() {
        long v4 = 0;
        Notifier v2 = null;
        if (this.mHandle != v4) {
            _release(this.mHandle);
            this.mHandle = v4;
        }

        this.mNotifyer = v2;
        this.mScreenshotListener = null;
    }

    protected int reset() {
        int v0 = this.mHandle == 0 ? -1 : _reset(this.mHandle);
        return v0;
    }

    public void rotateCamera(float arg3, float arg4) {
        _rotateCamera(this.mHandle, arg3, arg4);
    }

    public void seekTo(int arg3) {
        _seek(this.mHandle, arg3);
    }

    public void setCacheFile(String arg3, int arg4) {
        _setCacheFile(this.mHandle, arg3, arg4);
    }

    public static final void setCachePath(String arg0) {
        mCachePath = arg0;
    }

    public static final void setCrashPath(String arg0) {
        mCrashPath = arg0;
    }

    public void setDataSource(String arg5) {
        if (this.mHandle != 0) {
            _setDataSource(this.mHandle, arg5);
        }
    }

    public int setIntOption(int arg3, int arg4) throws RemoteException {
        if (arg3 == 22) {
            if (arg4 == 0) {
                Log.i("ttplayer", "will occur anr crash");
            } else if (arg4 == 2) {
                Log.i("ttplayer", "will occur native crash");
            } else if (arg4 == 1) {
                Process.killProcess(Process.myPid());
                System.exit(0);
                Log.i("ttplayer", "will occur java crash");
                if (((String) null).endsWith("java")) {
                    throw new RemoteException("simple crash occur");
                } else {
                    Log.i("ttplayer", "occur java crash end");
                }
            }
        }
        return _setIntValue(this.mHandle, arg3, arg4);
    }

    public static void setIsIPPlayer(boolean arg0) {
        mIsIPCPlayer = arg0;
    }

    public int setLongOption(int arg3, long arg4) {
        return _setLongValue(this.mHandle, arg3, arg4);
    }

    public void setLooping(int arg3) {
        _setLooping(this.mHandle, arg3);
    }

    public void setNotifyer(Notifier arg1) {
        this.mNotifyer = arg1;
    }

    public void setNotifyerState(long arg6) {
        this.mNotifyerState = arg6;
        _setIntValue(this.mHandle, 16, ((int) arg6));
    }

    public void setOnScreenshotListener(ScreenshotListener arg1) {
        this.mScreenshotListener = arg1;
    }

    public int setStringOption(int arg3, String arg4) {
        return _setStringValue(this.mHandle, arg3, arg4);
    }

    public int setSurfaceValue(long arg4) {
        return _setSurfaceValue(this.mHandle, arg4);
    }

    public static final void setTempFileDir(String arg0) {
        mAppPath = arg0;
    }

    protected int setVideoSurface(Surface arg7) {
        int v0 = -1;
        if (this.mHandle != 0) {
            v0 = _setVideoSurface(this.mHandle, arg7);
        }

        return v0;
    }

    public void setVolume(float arg3, float arg4) {
        _setVolume(this.mHandle, arg3, arg4);
    }

    protected int start() {
        int v0 = this.mHandle == 0 ? -1 : _start(this.mHandle);
        return v0;
    }

    protected int stop() {
        int v0;
        if (this.mHandle != 0) {
            _stop(this.mHandle);
            v0 = 0;
        } else {
            v0 = -1;
        }

        return v0;
    }

    public void takeScreenshot() {
        _takeScreenshot(this.mHandle);
    }

    public final void takeScreenshotComplete(Bitmap arg1) {
    }
}

